package com.herench.bs.handler;

import com.herench.bs.annotation.Handler;
import com.herench.bs.core.TokenProvider;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.support.TokenLifeCycle;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * Created by ThinkPad on 2017/8/24.
 */
@Component
@Handler(code = "100102",name="令牌获取")
public class TokenAcquisitionHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(final RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        String authCode = (String) requestMessage.getArgs().get("authCode");
        String password = (String) requestMessage.getArgs().get("password");
        Assert.isTrue(StringUtils.hasLength(authCode) && StringUtils.hasLength(password), "用户名或密码不能为空.");
        TokenLifeCycle tokenLifeCycle = TokenProvider.generatorToken(authCode);
        responseMessage = new ResponseMessage(requestMessage.getId());
        responseMessage.setDataAttribute("timeout", tokenLifeCycle.getTimeout());
        responseMessage.setDataAttribute("token", tokenLifeCycle.getTokenKey());
        responseMessage.setDataAttribute("connTime", tokenLifeCycle.getConnTime());
        responseMessage.setCode("0");
        responseMessage.setMessage("token生成成功.");
        return responseMessage;
    }
}
